package com.woniuxy.company5.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.company5.dto.LMacInformationDto;
import com.woniuxy.company5.dto.MacInfoDto;
import com.woniuxy.company5.dto.MacInformationDto;
import com.woniuxy.company5.dto.macListDto;
import com.woniuxy.company5.entity.MacImg;
import com.woniuxy.company5.entity.MacInformation;
import com.woniuxy.company5.entity.MacIo;
import com.woniuxy.company5.entity.MacList;
import com.woniuxy.company5.service.*;
import com.woniuxy.company5.util.Result;
import com.woniuxy.company5.vo.MacInformationVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/macInformation")
public class MacController {
    @Resource
    private MacIoService macIoService;
    @Resource
    private MacImgService macImgService;
    @Resource
    private MacListService macListService;
    @Resource
    private MacInformationService macInformationService;


    @GetMapping("{id}")
    public Result selOneMac(@PathVariable  Long id){
        MacInformationVo macInformationVo = macInformationService.selOneById(id);

        return Result.ok().data("numz",macInformationVo.getInventroy());
    }


    @PostMapping("selAllMac")
    public Result selAllMac(@RequestBody MacInfoDto macInfoDto){
        PageHelper.startPage(macInfoDto.getCurrentPage(),macInfoDto.getPageSize(),true);
        PageHelper.orderBy("id desc");
        List<MacInformationVo> macInformationVos = macInformationService.selAllSheBei(macInfoDto);
        PageInfo<MacInformationVo> pageInfo = new PageInfo<>(macInformationVos);
        return  Result.ok().data("macList",macInformationVos).data("total",pageInfo.getTotal());
    }

    @PostMapping("chuKuMac")
    public Result chuKu(@RequestBody macListDto macInformationVo){

     macIoService.chuKuService(macInformationVo);
        return Result.ok();
    }

    @PostMapping("")
    public Result addMacInfor(@RequestBody MacInformationDto macInformationDto){
        MacImg macImg = new MacImg();
        macImg.setImg1(macInformationDto.getMacImg1());
        macImgService.insertSelective(macImg);

        MacInformation macInformation = new MacInformation();
        macInformation.setImgId(macImg.getId().toString());
        macInformation.setTypeId(Integer.parseInt(macInformationDto.getTypeId()));
        macInformation.setWho(macInformationDto.getMacWho());
        macInformation.setState(macInformationDto.getState());
        macInformation.setProvenance(macInformationDto.getMacProvenance());
        macInformation.setLift(macInformationDto.getLife());
        macInformation.setDate(macInformationDto.getDate());
        macInformation.setBrand(macInformationDto.getBrand());
        macInformation.setCapacity(macInformationDto.getCapacity());
        macInformation.setModel(macInformationDto.getModel());
        macInformation.setInterfaceId(Integer.parseInt(macInformationDto.getInterfaceId()));
        macInformation.setSerialNum(macInformationDto.getSerialnum());
        macInformationService.insertSelective(macInformation);

        MacIo macIo = new MacIo();
        macIo.setInformationId(macInformation.getId());
        macIo.setNum(macInformationDto.getSerialnum()+new Date().getTime());
        macIo.setInDate(new Date());
        macIo.setBeiyong1(macInformationDto.getInventory().toString());
        Integer who = macInformationDto.getMacWho();
        Integer provenance = macInformationDto.getMacProvenance();

        if (who == 2){
            macIo.setType(2);
        }else {
            if (provenance == 1) {
                macIo.setType(1);
            } else if(provenance == 2){
                macIo.setType(3);
            }
        }
        macIoService.insertSelective(macIo);

        MacList macList = new MacList();
        macList.setType(macInformationDto.getTypeId());
        macList.setInformationId(macInformation.getId());
        macList.setInventory(macInformationDto.getInventory());
        macListService.insertSelective(macList);

        return Result.ok();
    }
}
